package bb.lanxing.model.database;

import java.util.ArrayList;
import java.util.List;

import org.litepal.LitePal;

import com.baidu.mapapi.model.LatLng;

import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;

import bb.lanxing.util.BiCiCoorConverter;

public class RouteBookPoint extends LitePalTableBase implements Parcelable, IRouteBookPoint {
    public static final double BAIDU_MAX = 200.0d;

    public static final Creator<RouteBookPoint> CREATOR = new Creator<>() {
        @Override
        public RouteBookPoint createFromParcel(Parcel parcel) {
            return new RouteBookPoint(parcel);
        }

        @Override
        public RouteBookPoint[] newArray(int i) {
            return new RouteBookPoint[i];
        }
    };

    private static SQLiteDatabase db;
    private double altitude;
    private double distance;
    private double latitude;
    private double longitude;
    private long lushuId;

    public RouteBookPoint() {
    }

    protected RouteBookPoint(Parcel parcel) {
        this.lushuId = parcel.readLong();
        this.latitude = parcel.readDouble();
        this.longitude = parcel.readDouble();
        this.altitude = parcel.readDouble();
        this.distance = parcel.readDouble();
    }

    public static List<RouteBookPoint> getByLushuId(long lushuId) {
        return LitePal.where("lushuId=?", String.valueOf(lushuId)).order("id").find(RouteBookPoint.class);
    }

    public static List<LatLng> getBaiduStaticMapPointsBy(long j, long j2) {
        List<RouteBookPoint> byLushuId;
        double d = j2;
        if (d > BAIDU_MAX) {
            int ceil = (int) Math.ceil(d / BAIDU_MAX);
            List<RouteBookPoint> byLushuId2 = getByLushuId(j);
            byLushuId = new ArrayList<>();
            for (int i = 0; i < byLushuId2.size(); i += ceil) {
                byLushuId.add(byLushuId2.get(i));
            }
            RouteBookPoint lushuPoint = byLushuId2.get(byLushuId2.size() - 1);
            if (!byLushuId.contains(lushuPoint)) {
                byLushuId.add(lushuPoint);
            }
        } else {
            byLushuId = getByLushuId(j);
        }
        ArrayList<LatLng> arrayList = new ArrayList<>();
        for (RouteBookPoint lushuPoint2 : byLushuId) {
            arrayList.add(
                    BiCiCoorConverter.earth2Baidu(new LatLng(lushuPoint2.getLatitude(), lushuPoint2.getLongitude())));
        }
        return arrayList;
    }

    public static List<LatLng> getLatLngForGoogleStaticMap(long j) {
        List<RouteBookPoint> byLushuId;
        double countById = getCountById(j);
        if (countById > 100.0d) {
            int ceil = (int) Math.ceil(countById / 100.0d);
            List<RouteBookPoint> byLushuId2 = getByLushuId(j);
            byLushuId = new ArrayList();
            for (int i = 0; i < byLushuId2.size(); i += ceil) {
                byLushuId.add(byLushuId2.get(i));
            }
            RouteBookPoint lushuPoint = byLushuId2.get(byLushuId2.size() - 1);
            if (!byLushuId.contains(lushuPoint)) {
                byLushuId.add(lushuPoint);
            }
        } else {
            byLushuId = getByLushuId(j);
        }
        ArrayList<LatLng> arrayList = new ArrayList<>();
        for (RouteBookPoint lushuPoint2 : byLushuId) {
            arrayList.add(
                    BiCiCoorConverter.earth2Common(new LatLng(lushuPoint2.getLatitude(), lushuPoint2.getLongitude())));
        }
        return arrayList;
    }

    public static long getCountById(long j) {

        return LitePal.where("lushuId=?", String.valueOf(j)).count(RouteBookPoint.class);

        // return Select.from(LushuPoint.class).where("lushu_id = ? ", new String[]{String.valueOf(j)}).count();

    }

    public static void deleteByRouteBookId(long j) {
        LitePal.deleteAll(RouteBookPoint.class, "lushuId=?", String.valueOf(j));
    }

    public static void savePoints(List<RouteBookPoint> list) {
        LitePal.saveAll(list);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public long getLushuId() {
        return this.lushuId;
    }

    public void setLushuId(long j) {
        this.lushuId = j;
    }

    public void setLatlng(LatLng latLng) {
        this.latitude = latLng.latitude;
        this.longitude = latLng.longitude;
    }

    @Override
    public double getAltitude() {
        return this.altitude;
    }

    public void setAltitude(double d) {
        this.altitude = d;
    }

    @Override
    public double getDistance() {
        return this.distance;
    }

    @Override
    public void setDistance(double d) {
        this.distance = d;
    }

    @Override
    public double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double d) {
        this.latitude = d;
    }

    @Override
    public double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double d) {
        this.longitude = d;
    }

    @Override
    public LatLng getLatLng() {
        return new LatLng(this.latitude, this.longitude);
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(this.lushuId);
        parcel.writeDouble(this.latitude);
        parcel.writeDouble(this.longitude);
        parcel.writeDouble(this.altitude);
        parcel.writeDouble(this.distance);
    }
}
